package me.xingyannian.hope.service.impl;

import me.xingyannian.hope.entity.Menu;
import me.xingyannian.hope.entity.Role;
import me.xingyannian.hope.repository.MenuRepository;
import me.xingyannian.hope.repository.UserRepository;
import me.xingyannian.hope.service.MenuService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class MenuServiceImpl implements MenuService {
    private MenuRepository menuRepository;
    private UserRepository userRepository;

    public MenuServiceImpl(MenuRepository menuRepository, UserRepository userRepository) {
        this.menuRepository = menuRepository;
        this.userRepository = userRepository;
    }

    @Override
    public Menu create(Menu resource) {
        return this.menuRepository.save(resource);
    }

    @Override
    public Boolean existsMenuByTitle(String title) {
        return this.menuRepository.existsMenuByTitle(title);
    }

    @Override
    public Menu getMenuByTitle(String title) {
        return this.menuRepository.getMenuByTitle(title);
    }

    @Override
    public List<Menu> getAllMenu() {
        return this.menuRepository.findAll();
    }

    @Override
    public List<Menu> getAllMenuByRoles(List<Role> roles) {
        return this.menuRepository.getMenusByRolesIn(roles);
    }

    @Override
    public Set<Menu> getMenusByLoginId(String loginId) {
        var user = this.userRepository.getUserByLoginId(loginId);
        var roles = user.getRoles();
        var menus = this.menuRepository.getMenusByRolesIn(roles);
        return menus;
    }
}
